package com.sjzsqjy.asdtiang.db;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sjzsqjy.asdtiang.mysql.HosUser;
import com.sjzsqjy.asdtiang.mysql.Info;
import com.sjzsqjy.asdtiang.util.MD5;
/**
 * 
 * <bean id="shareServer" class="org.jinjin.trip.server.ShareServer">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
 **/
public  class ShareService extends HibernateDaoSupport {
	public void update(Object obj) {
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		Transaction tran=session.beginTransaction();
		session.update(obj);
		tran.commit();
		session.flush();
		session.close();
	}
	public void save(Object obj) {
		HibernateTemplate template=this.getHibernateTemplate();
		Session session=template.getSessionFactory().openSession();
		Transaction tran=session.beginTransaction();
		session.save(obj);
		tran.commit();
		session.flush();
		session.close();		
	}
	public void delete(Object obj){
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		Transaction tran=session.beginTransaction();
		session.delete(obj);
		tran.commit();
		session.flush();
		session.close();
	}
	public HosUser login(String username, String password) {
		Object[] values = new String[] { username,
				MD5.getMD5(password.getBytes()) };
		System.out.println(MD5.getMD5(password.getBytes()));
		List ls = getHibernateTemplate().findByNamedQuery("login", values);
		return ls.size() == 1 ? (HosUser) ls.get(0) : null;
	}
	public Object getObjectById(String className,Object id){
		Object[] values = new Object[] {id};
		List ls = getHibernateTemplate().find("from "+className+" where id=?", values);
		return ls.size() == 1 ?  ls.get(0) : null;	
	}
	public List listAllByObjectName(String name,String order){
		List ls = getHibernateTemplate().find("from "+name+" "+order);
		return ls!=null?  ls : null;	
	}
	
}

